<style>
    .layui-form-label.required:before { /* 这种写法也可以*/
        content: ' *';
        color: red;
    }

    .open-form .layui-input-block{
        margin-left: 145px;
    }
    .open-form .layui-form-label {
        width: 115px;
    }

    .addProductionPlanForm-title .title, .examineProductionPlanForm-title .title {
        display: inline-block;
        margin: 20px;
        font-size: 16px;
        font-weight: bold;
        color: #333333;
        line-height: 19px;
        position: relative;
        padding: 6px 0 19px 0;
    }

    .addProductionPlanForm-title .title:after, .examineProductionPlanForm-title .title:after {
        content: '';
        display: block;
        position: absolute;
        left: 0;
        bottom: 0;
        width: calc(100% + 6px);
        height: 3px;
        background: #0A8B0A;
    }
</style>
<script type="text/javascript" src="../../../admin/assets/js/pdfobject.min.js"></script>
<!-- 表单弹窗 -->
<form lay-filter="requirePlanInfoFrom" class="layui-form open-form">
    <div class="open-model">
        <div class="open-auto">

            <div class="layui-form-item layui-row">

                <div class="layui-col-md3">
                    <label class="layui-form-label">PlantCode：</label>
                    <div class="layui-input-block">
                        <input type="text" id="plantCode" name="plantCode"   class="layui-input"
                               placeholder="PlantCode">
                    </div>
                </div>

                <div class="layui-col-md3">
                    <label class="layui-form-label">工厂名称：</label>
                    <div class="layui-input-block">
                        <input type="text" id="factoryName" name="factoryName"   class="layui-input"
                               placeholder="工厂名称">
                    </div>
                </div>


<!--

                <div class="layui-col-md3">
                    <label class="layui-form-label">预测需求时间：</label>
                    <div class="layui-input-block">
                        <input type="text" id="receiveMouthDate" name="receiveMouthDate"   class="layui-input"
                               placeholder="预测需求时间">
                    </div>
                </div>
-->


            </div>



            <div class="examineProductionPlanForm-title">
                <h3 class="title">生产物料</h3>
            </div>
            <div class="layui-form-item layui-row">
                <button type="button" id="addPlanItem" class="layui-btn" lay-filter="addPlanItem"
                >添加生产物料
                </button>
                <div id="demandDay">
                    <table class="layui-table" id="requireItemTable" lay-filter="requireItemTable"></table>
                </div>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="saveProductFormSubmit" lay-submit>提交</button>
        </div>

    </div>


</form>

<script type="text/html" id="viewAttachmentHtml">
    <div id="example1" style="height: 100%;"></div>
</script>

<script type="text/html" id="productTableBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delProductItem">删除</a>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'laydate', 'admin', 'laytpl', 'upload', 'config'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var config = layui.config;
        var table = layui.table;
        var admin = layui.admin;
        var form = layui.form;
        var laydate = layui.laydate;

        let skuInfoObj = new Map()
        let planItemData = [];

        //初始化计划表
        table.render({
            elem: '#requireItemTable',
            data: planItemData,
            id: 'requireItemTable',
            limit: 9999,
            cols: [[
                  {type: 'numbers', title: '序号', width: 80}
                 ,{field: 'materialCode', align: 'center', edit: function(d) { return d.isEditable ? 'text' : false; }, title: '物料编码'}
                , {field: 'drawingNumber', align: 'center', edit: 'text', title: '图纸编号'}
                , {field: 'materialDescribe', align: 'center', edit: 'text', title: '组件描述'}

                , {field: 'belongType', align: 'center', edit: 'text', title: '所属类别'}
                , {field: 'materialSimplifiedCode', align: 'center', edit: 'text', title: '物料简码'}

                , {field: 'purchaseTissue', align: 'center', edit: 'text', title: '采购组织编号'}
                , {field: 'purchaseTissueName', align: 'center', edit: 'text', title: '采购组织名称'}

                , {field: 'productLine', align: 'center', edit: 'text', title: '生产线'}
                , {field: 'demandQuantity', align: 'center', edit: 'text', title: '需求数量'}
                , {field: 'materialUnit', align: 'center', edit: 'text', title: '单位'}
                , {field: 'receiveMouthDate', align: 'center', edit: 'text', title: '接收月份'}
                , {align: 'center', toolbar: '#productTableBar', title: '操作'}
            ]]
        });

        table.on('rowDouble(requireItemTable)', function(obj) {
            let birthdayCell = $(obj.tr).find('.date-cell');
            laydate.render({
                elem: birthdayCell[0], // 绑定到生日单元格
                type: 'month', // 类型为月
                format: 'yyyy-MM', // 格式
                range: false, // 不允许选择范围
                done: function (value, date) {
                    obj.update({
                        planTime: value
                    });
                    // 重新加载表格数据
                    table.reload('planItemData', {
                        data: planItemData // 传入更新后的数据
                    });
                }
            });
            // 手动触发日期选择器
            birthdayCell.click();
        });

        //数据
        let tempData = admin.getTempData("requirePlanInfoData");

        laydate.render({
            elem: '#planReceiveTime'
            , type: 'datetime'
        });

     /*   admin.getDictList("materialTypeId", "pro_customer_product_material",tempData.materialTypeId , "物料类型");*/
         //表单提交事件
        form.on('submit(saveProductFormSubmit)', function (d) {

            layer.load(2);
            let cacheElement = table.cache['requireItemTable'];
            let data = [];
            for (const key in cacheElement) {
                if(cacheElement[key].materialCode != undefined){
                    data.push(cacheElement[key])
                }
            }
           /* tempData.factoryName = d.field.factoryName;
            tempData.plantCode = d.field.plantCode;*/
           /* d.receiveMouthDate = d.field.receiveMouthDate;*/
            d.field.requirePlanItemList  = data;

            admin.req('production/productionPlan/admin/requirePlan/savePlan', JSON.stringify(d.field), function (res) {
                layer.closeAll('loading');
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload('requirePlanInfo');
                    layer.closeAll('page');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST', true, 'application/json; charset=utf-8');
            return false;
        });

        //移除某一行
        table.on('tool(requireItemTable)', function (obj) {
            if (obj.event === 'delProductItem') { //删除
                obj.del(); // 删除当前行
                // 获取表格数据
                planItemData = table.cache['requireItemTable'];
                table.reload('requireItemTable', {
                    data: planItemData
                });
            }
        });


        $("#addPlanItem").click(function () {
            planItemData = table.cache['requireItemTable'];
            let newRow = {
                materialCode: "",
                drawingNumber: "",

                materialDescribe: "",
                belongType: "",

                materialSimplifiedCode: "",
                purchaseTissue: "",

                purchaseTissueName: "",
                productLine: "",

                demandQuantity: "",
                materialUnit: ""
            };
            planItemData.push(newRow);
            table.reload('requireItemTable', {
                data: planItemData
            })
        })



    });
</script>